/**
 * Created by Oren on 2015/10/30.
 */
+function(){
    var player = null,
        currentTarget = null,
        progress = $('.progress'),

        resetTarget = function(elem){
            $(elem).closest(".play-items").removeClass("playing");
            $(elem).removeClass("play");
            $(elem).removeClass("pause");
        },
        setPlaying = function(elem){
            $(elem).closest(".play-items").addClass("playing").find(".progress");
            progress.find(".progress-bar").width(0);
            $(elem).removeClass("pause");
            $(elem).addClass("play");
        },
        initPlayer = function(){
            player = document.getElementById("audioId");
            //document.body.appendChild(player);
            //player.style.display = "none";
            $(player).bind({
                ended: function(e){
                    if(currentTarget != null){
                        $(currentTarget).addClass("pause");
                    }
                },
                error: function(e){
                    if(currentTarget != null){
                        $(currentTarget).addClass("play");
                        currentTarget.errorPlay = true;
                        $(currentTarget).closest(".play-items").removeClass("playing");
                        currentTarget = null;
                    }
                },
                pause: function(e){
                    if(currentTarget != null){
                        $(currentTarget).removeClass("play");
                        $(currentTarget).addClass("pause");
                    }
                },
                play: function(e){
                },
                playing: function(e){
                    if(currentTarget != null){
                        $(currentTarget).addClass("play");
                    }
                },
                timeupdate: function(e){
                    if(player.duration>0){
                        $(".progress-bar").width((100*player.currentTime/player.duration)+"%");
                        var curr=(player.currentTime/100).toFixed(2)
                        $(".currenttime").text((player.currentTime/100).toFixed(2));
                        $(".totaltime").text((player.duration/100).toFixed(2));
                    }
                },
                waiting: function(e){
                    if(currentTarget != null){
                        $(currentTarget).addClass("pause");
                    }
                }
            });
        },
        play = function(elem){
            if(player === null){
                initPlayer();
            }
            if(currentTarget === elem){
                if(player.paused){
                    player.play();
                }else{
                    player.pause();
                }
            }
            else{
                currentTarget && resetTarget(currentTarget);
                player.pause();
                currentTarget = elem;
                player.src = elem.getAttribute("href");
                setPlaying(elem);
                player.play();
            }
        }
        ;
    $(function(){
        $(".play-items a.play-btn").on("click", function(e){
            if(this.errorPlay){
                currentTarget && resetTarget(currentTarget);
            }else{
                play(this);
            }
            return false;
        });

        var videoPlayer = $("#videoPlayer");
        var audioPlayer = $("#audioPlayer");
        if(videoPlayer.size() > 0){
            videoPlayer.bind({
                ended: function(e){
                },
                error: function(e){
                    //alert("播放视频错误！");
                },
                pause: function(e){

                },
                play: function(e){
                    if(audioPlayer.size() > 0 && !audioPlayer.get(0).paused){
                        audioPlayer.get(0).pause();
                    }
                },
                waiting: function(e){
                }
            });
        }
        if(audioPlayer.size() > 0){
            audioPlayer.bind({
                ended: function(e){

                },
                error: function(e){
                    //alert("播放音频错误！");
                },
                pause: function(e){

                },
                play: function(e){
                    if(videoPlayer.size() > 0 && !videoPlayer.get(0).paused){
                        videoPlayer.get(0).pause();
                    }
                },
                waiting: function(e){

                }
            });
        }
    });
}();